<template>
  <div class="top">
    <div class="notice">
      <div class="scroll-container">
        <i class="iconfont">&#xe609;</i>
        <p>：{{ currentAnnouncement }}</p>
      </div>
    </div>
    <div class="content">
      <div class="left">
        <i class="iconfont">&#xe6e5;</i>
        <p>智慧医疗服务平台</p>
      </div>
      <div class="right">
        <p style="margin-right: 15px;" @click="showHelpCenter">帮助中心</p>
        <template v-if="isLoggedIn">
          <div class="user-avatar-container">
            <img :src="userAvatar || ''" alt="用户头像" class="user-avatar" @click="toggleDropdown" />
            <div v-show="showDropdown" class="dropdown-menu">
              <div class="dropdown-item" @click="handleMessage"><router-link
                  to="/front/personMessage">个人信息</router-link></div>
              <div class="dropdown-item" @click="handleLogout">退出登录</div>
            </div>
          </div>
        </template>
        <template v-else>
          <p @click="login">登录/注册</p>
        </template>
      </div>
    </div>
  </div>
  <!-- 帮助中心弹出框 -->
  <el-dialog v-model="dialogVisible" width="50%">
    <div class="dialog-content">
      <h3>联系电话</h3>
      <p>客服热线: 123-456-7890</p>
      <p>技术支持: 098-765-4321</p>
    </div>
    <div class="dialog-content">
      <h3>小提示</h3>
      <ul>
        <li>请确保您的网络连接正常。</li>
        <li>如果您遇到任何问题，请随时联系我们的客服。</li>
        <li>我们的服务时间为周一至周六，9:00 - 21:00。</li>
      </ul>
    </div>
  </el-dialog>
</template>

<script setup lang="ts">
import { ref, onMounted, onUnmounted } from 'vue';
import { useRouter } from 'vue-router';
import axios from 'axios';

const router = useRouter();

// 公告数组
const announcements = ref<string[]>([]);

const isLoggedIn = ref(false);//登录和用户头像切换
const showDropdown = ref(false);

// 获取公告信息
const fetchAnnouncements = async () => {
  try {
    const token = localStorage.getItem('token');
    const response = await axios.get('/api/announcement/list', {
      headers: {
        'Authorization': `Bearer ${token}`
      }
    });
    console.log("公告111：", response.data)
    if (response.data && response.data.rows.length > 0) {
      announcements.value = response.data.rows.map((announcement: any) => announcement.title || announcement.content);
    } else {
      console.error('未获取到公告信息');
    }
  } catch (error) {
    console.error('获取公告信息失败:', error);
  }
};

// 当前显示的公告内容
const currentAnnouncement = ref('');

const userAvatar = ref<string | undefined>();

// 切换下拉菜单显示状态
const toggleDropdown = () => {
  showDropdown.value = !showDropdown.value;
};
// 从 localStorage 中读取 userAvatar
onMounted(() => {
  const avatar = localStorage.getItem('avatar');
  console.log('头像：', avatar);
  userAvatar.value = avatar || undefined; // 将 null 转换为 undefined
  isLoggedIn.value = localStorage.getItem('isLoggedIn') === 'true'; // 将 string 转换为 boolean
});

// 当前公告索引
let currentAnnouncementIndex = 0;

// 当前字符索引
let currentCharIndex = 0;

// 定时器
let intervalId: NodeJS.Timeout | null = null;

// 显示公告内容的函数
const displayAnnouncement = () => {
  if (announcements.value.length === 0) return; // 如果没有公告，直接返回

  if (currentCharIndex < announcements.value[currentAnnouncementIndex].length) {
    currentAnnouncement.value += announcements.value[currentAnnouncementIndex][currentCharIndex];
    currentCharIndex++;
  } else {
    // 当前公告显示完毕，切换到下一个公告
    currentAnnouncementIndex++;
    currentCharIndex = 0;
    currentAnnouncement.value = '';
    if (currentAnnouncementIndex >= announcements.value.length) {
      currentAnnouncementIndex = 0; // 重新开始
    }
  }
};

// 挂载后启动定时器
onMounted(() => {
  fetchAnnouncements(); // 获取公告信息
  intervalId = setInterval(displayAnnouncement, 150); // 启动公告滚动
});

// 组件卸载前清除定时器
onUnmounted(() => {
  if (intervalId) {
    clearInterval(intervalId);
  }
});

// 点击登录/注册按钮时弹出对话框
const login = () => {
  router.push({ path: '/login' });
};

const handleMessage = () => {
  showDropdown.value = false;
}

const handleLogout = () => {
  localStorage.removeItem('isLoggedIn');
  localStorage.removeItem('nickName');
  localStorage.removeItem('token');
  localStorage.removeItem('userAvatar');

  userAvatar.value = '';
  isLoggedIn.value = false;
  showDropdown.value = false; // 隐藏下拉菜单
  router.push('/').then(() => {
    // 跳转成功后刷新页面
    window.location.reload();
  });
};

// 控制帮助中心弹出框的显示状态
const dialogVisible = ref(false);

// 显示帮助中心弹出框
const showHelpCenter = () => {
  dialogVisible.value = true;
};
</script>


<style scoped lang="scss">
.top {
  position: fixed;
  z-index: 999;
  width: 100%;
  height: 70px;
  background-color: #fff;
  display: flex;
  flex-direction: column; // 设置为列方向
  justify-content: flex-start;
  align-items: center;

  .notice {
    width: 1200px;
    color: #fd2a2a; // 公告文字颜色
    font-size: 16px;
    overflow: hidden; // 隐藏溢出内容

    .scroll-container {
      width: 100%;
      height: 40px; // 设置滚动容器的高度
      overflow-x: scroll; // 设置垂直滚动
      white-space: nowrap; // 防止文本换行
    }

    p {
      margin: 0; // 去除段落默认的上下外边距
      display: inline-block; // 使段落元素水平排列
    }
  }

  .content {
    width: 1200px;
    height: 70px;
    background-color: #fff;
    display: flex;
    justify-content: space-between;

    .left {
      display: flex;
      justify-content: center; //
      align-items: center;
      color: rgba(26, 177, 228, 0.71);
      font-weight: 550;

      i {
        font-size: 50px;
        margin-right: 15px;
      }

      p {
        font-size: 20px;
      }

    }

    .right {
      cursor: pointer;
      display: flex;
      justify-content: center;
      align-items: center;
    }
  }
}

/* 自定义 el-dialog 样式 */
.el-dialog {
  border-radius: 10px; // 圆角边框
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1); // 阴影效果
  background-color: #f9f9f9; // 背景颜色

  .dialog-content {
    margin-bottom: 20px;

    h3 {
      margin-bottom: 10px;
      color: #409EFF; // 小标题颜色
      font-size: 18px;
    }

    p {
      margin: 5px 0;
      font-size: 16px;
    }

    ul {
      list-style-type: disc;
      padding-left: 20px;

      li {
        margin-bottom: 5px;
        font-size: 16px;
      }
    }
  }
}

.user-avatar-container {
  position: relative;
}

.user-avatar {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  object-fit: cover;
  cursor: pointer;
}

.dropdown-menu {
  position: absolute;
  top: 50px;
  /* 调整下拉菜单与头像的距离 */
  right: 0;
  background-color: #fff;
  border: 1px solid #ddd;
  border-radius: 4px;
  box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
  z-index: 1000;
  /* 确保下拉菜单显示在其他内容之上 */
  min-width: 120px;
  /* 设置最小宽度 */
}

.dropdown-item {
  padding: 8px 16px;
  font-size: 14px;
  color: #333;
  cursor: pointer;

  &:hover {
    background-color: #f5f5f5;
  }
}
</style>
